<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日记列表</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/logout.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/touxiang.png" alt="">
        <span class="title">我的日记系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写日记</a>
        <a href="javascript:logout() ">注销</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/doge.jpg" class="avtar" alt="">
                <h3>鸽鸽爱编程</h3>
                <a href="https://gitee.com/shitaoup">gitee 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
            <div class="blank">
                <span>这是一个空白内容，用于存放广告</span>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div id="articleListDiv" class="container-right">
        </div>
    </div>
    <script>
        //初始方法（打开这个页面就要调用这个方法）
        function init() {
            jQuery.ajax({
                url:"/article/mylist",
                type:"GET",
                data:{},
                success:function(ret) {
                    if(ret.code == 200) {
                        // 请求成功
                        var createHtml = "";
                        var articleList = ret.data;
                        if(articleList == null || articleList.length == 0) {
                            //未发表文章
                            createHtml += '<h3 style="color: orange;text-align: center;">暂无内容，请"+"<a href="blog_add.html">添加</a>！</h3>';
                        }else {
                            for(var i = 0;i < articleList.length;i++) {
                                var art = articleList[i];
                                createHtml += '<div class="blog">';
                                createHtml += '<div class="title">'+art.title+'</div>';
                                createHtml += '<div class="date">'+art.createtime+'</div>';
                                createHtml += '<div class="desc">';
                                createHtml += art.content;
                                createHtml += '</div>';
                                createHtml += '<a href="blog_content.html?aid='+art.id+'" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp';
                                createHtml += '<a href="blog_edit.html?aid='+art.id+'" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp';
                                createHtml += '<a href="javascript:del('+art.id+')" class="detail">删除 &gt;&gt;</a>';
                                createHtml += '</div>';
                            }
                        }
                        jQuery("#articleListDiv").html(createHtml);
                    }else {
                        alert("操作失败"+ret.msg);
                    }
                }
            });
        }
        init();

        //通过文章id进行删除操作
        function del(aid) {
            // 1.参数校验
            if(aid == "" || aid <=0) {
                alert("参数错误");
                return false;
            }
            // 2.将数据返回给后端，从而进行删除操作
            jQuery.ajax({
                url:"/article/del",
                type:"POST",
                data:{
                    "aid":aid
                },
                success:function(ret) {
                    // 3.将结果展示给用户
                    if(ret.code==200 && ret.data==1) {   
                        alert("删除成功!");
                        // 刷新当前页面
                        location.href = location.href;
                    }else{
                        alert("删除失败!"+ret.msg);
                    }
                }
            });
        }
    </script>
</body>

</html>